package com.question.business.role.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.role.model.ao.*;
import com.question.business.role.model.vo.RoleAddVO;
import com.question.business.role.model.vo.RoleDetailVO;
import com.question.business.role.model.vo.RoleDropDownVO;
import com.question.business.role.model.vo.RoleVO;
import com.question.business.role.service.IRoleService;
import com.question.common.annotation.DataConvert;
import com.question.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 角色接口
 *
 * @author star
 * @since 2023-09-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
public class RoleController {

    private final IRoleService service;


    /**
     * 角色分页查询
     *
     * @param ao 请求参数
     * @return Page<RoleVO> 响应参数
     */
    @GetMapping("/page")
    public Page<RoleVO> queryRolePage(@Validated RolePageAO ao) {
        return service.queryRolePage(ao);
    }

    /**
     * 角色明细查询
     *
     * @param id 角色ID
     * @return RoleDetailVO 响应参数
     */
    @DataConvert
    @GetMapping("/detail")
    public RoleDetailVO queryRoleDetail(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        return service.queryRoleDetail(id);
    }

    /**
     * 角色新增数据
     *
     * @param ao 请求参数
     * @return RoleAddVO 响应参数
     */
    @PostMapping("/add")
    public RoleAddVO addRole(@Validated @RequestBody RoleAddAO ao) {
        return service.addRole(ao);
    }

    /**
     * 角色修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updateRole(@Validated @RequestBody RoleUpdateAO ao) {
        service.updateRole(ao);
    }

    /**
     * 角色删除数据
     *
     * @param id 角色ID
     */
    @DeleteMapping("/delete")
    public void deleteRole(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        service.deleteRole(id);
    }

    /**
     * 角色批量删除数据
     *
     * @param ids 请求参数
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchRole(@RequestParam @Validated @NotNull(message = "删除数据不能为空") @Size(min = 1, message = "删除数据不能为空") List<@NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String> ids) {
        service.deleteBatchRole(ids);
    }

    /**
     * 角色导出数据
     *
     * @param ao       请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportRole(RolePageAO ao, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("角色-" + DateUtil.getStringTime() + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportRole(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 角色类型启用、停用
     *
     * @param ao 数据
     */
    @PutMapping("/enable")
    public void enableRole(@Validated @RequestBody RoleEnabledAO ao) {
        service.enableRole(ao);
    }

    /**
     * 角色下拉列表查询
     *
     * @param ao 请求参数
     * @return List<RoleDropDownVO> 响应参数
     */
    @GetMapping("/drop/down")
    public List<RoleDropDownVO> queryRoleDropDown(@Validated RoleDropDownAO ao) {
        return service.queryRoleDropDown(ao);
    }
}
